
package com.zhike.service.im.client;

import com.zhike.service.im.client.core.AutoReLoginDaemon;
import com.zhike.service.im.client.core.KeepAliveDaemon;
import com.zhike.service.im.client.core.LocalSocketProvider;
import com.zhike.service.im.client.core.QoS4ReciveDaemon;
import com.zhike.service.im.client.core.QoS4SendDaemon;
import com.zhike.service.im.client.event.ChatBaseEvent;
import com.zhike.service.im.client.event.ChatMessageEvent;
import com.zhike.service.im.client.event.MessageQoSEvent;

import org.springframework.stereotype.Component;

import com.zhike.mobileimsdk.server.protocal.c.PLoginInfo;

@Component
public class ClientCoreSDK {

	private final static String TAG = ClientCoreSDK.class.getSimpleName();

	public static boolean DEBUG = true;
	public static boolean autoReLogin = true;
	private static ClientCoreSDK instance = null;

	private boolean _init = false;
	private boolean connectedToServer = true;
	private boolean loginHasInit = false;
	private PLoginInfo currentLoginInfo = null;

	private ChatBaseEvent chatBaseEvent = null;
	private ChatMessageEvent chatMessageEvent = null;
	private MessageQoSEvent messageQoSEvent = null;

	public static ClientCoreSDK getInstance() {
		if (instance == null) {
			synchronized (ClientCoreSDK.class) {
				if (instance == null) {
					instance = new ClientCoreSDK();
				}
			}
		}
		return instance;
	}

	private ClientCoreSDK() {

	}

	public void release() {
		this.setConnectedToServer(false);

		LocalSocketProvider.getInstance().closeLocalSocket();
		AutoReLoginDaemon.getInstance().stop();
		QoS4SendDaemon.getInstance().stop();
		KeepAliveDaemon.getInstance().stop();

		QoS4ReciveDaemon.getInstance().stop();

		QoS4SendDaemon.getInstance().clear();
		QoS4ReciveDaemon.getInstance().clear();

		_init = false;
		this.setLoginHasInit(false);
		// this.setConnectedToServer(false);
	}

	public void setCurrentLoginInfo(PLoginInfo currentLoginInfo) {
		this.currentLoginInfo = currentLoginInfo;
	}

	public PLoginInfo getCurrentLoginInfo() {
		return this.currentLoginInfo;
	}

	public void saveFirstLoginTime(long firstLoginTime) {
		if (this.currentLoginInfo != null)
			this.currentLoginInfo.setFirstLoginTime(firstLoginTime);
	}

	@Deprecated
	public String getCurrentLoginUserId() {
		return this.currentLoginInfo.getLoginUserId();
	}

	@Deprecated
	public String getCurrentLoginToken() {
		return this.currentLoginInfo.getLoginToken();
	}

	@Deprecated
	public String getCurrentLoginExtra() {
		return this.currentLoginInfo.getExtra();
	}

	public boolean isLoginHasInit() {
		return loginHasInit;
	}

	public ClientCoreSDK setLoginHasInit(boolean loginHasInit) {
		this.loginHasInit = loginHasInit;
		return this;
	}

	public boolean isConnectedToServer() {
		return connectedToServer;
	}

	public void setConnectedToServer(boolean connectedToServer) {
		this.connectedToServer = connectedToServer;
	}

	public boolean isInitialed() {
		return this._init;
	}

	public void setChatBaseEvent(ChatBaseEvent chatBaseEvent) {
		this.chatBaseEvent = chatBaseEvent;
	}

	public ChatBaseEvent getChatBaseEvent() {
		return chatBaseEvent;
	}

	public void setChatMessageEvent(ChatMessageEvent chatMessageEvent) {
		this.chatMessageEvent = chatMessageEvent;
	}

	public ChatMessageEvent getChatMessageEvent() {
		return chatMessageEvent;
	}

	public void setMessageQoSEvent(MessageQoSEvent messageQoSEvent) {
		this.messageQoSEvent = messageQoSEvent;
	}

	public MessageQoSEvent getMessageQoSEvent() {
		return messageQoSEvent;
	}
}
